<template>
  <div class="app-main">
    <div class="app-main-header">
      <el-page-header @back="reset('ruleForm')" content="编辑定时任务" />
      <div class="header-right">
        <el-button size="mini" @click="reset('ruleForm')">取 消</el-button>
        <el-button size="mini" type="primary" :loading="loading" @click="update('ruleForm')" >确 定</el-button>
      </div>
    </div>
    <div class="app-main-contain">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调用目标字符串" prop="invoke_target">
              <el-select v-model="ruleForm.invoke_target" placeholder="请选择" style="width:100%;">
                <el-option label="task.clearTmpFile" value="task.clearTmpFile"></el-option>
                <el-option label="task.notification" value="task.notification"></el-option>
                <el-option label="task.clearLogFile" value="task.clearLogFile"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="cron执行表达式" prop="cron_expression">
              <el-input v-model="ruleForm.cron_expression"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通知邮箱" prop="email">
              <el-input v-model="ruleForm.email"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div class="contain-editor">
        <div class="editor-nav">
          <span>通知内容</span>
        </div>
        <dmavon-editor :content.sync="ruleForm.content" />
      </div>
    </div>
  </div>
</template>

<script>
import dmavonEditor from '@/components/mavonEditor'
export default {
  name: 'editTask',
  components: {
    dmavonEditor,
  },
  data () {
    return {
      loading: false,
      ruleForm: {
        name: '',
        invoke_target: '',
        cron_expression: '',
        email: '',
        status: true,
        content: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
        invoke_target: [
          { required: true, message: '请选择调用目标字符串', trigger: 'change' }
        ],
        cron_expression: [
          { required: true, message: '请输入cron执行表达式', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.detaliInfo()
  },
  methods: {
    reset(formName) {
      this.$refs[formName].resetFields()
      this.$router.go(-1)
    },
    detaliInfo () { // 根据id获取详情
      this.$API({
        name: 'DetailTask',
        paths: [this.$route.params.id],
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          this.ruleForm = res.data.data
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      })
    },
    update (formName) { // 更新
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            id: this.ruleForm.id,
            name: this.ruleForm.name,
            invoke_target: this.ruleForm.invoke_target,
            cron_expression: this.ruleForm.cron_expression,
            email: this.ruleForm.email,
            status: this.ruleForm.status,
            content: this.ruleForm.content,
            uid: this.$Cookies.get('uid')
          }
          this.$API({
            name: 'UpdateTask',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$router.go(-1)
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.app-main {
  .app-main-header {
    height: 31px;
    width: 100%;
    flex-shrink: 0;
    display: flex;
    justify-content: space-between;
  }
  .app-main-contain {
    padding: 10px;
    overflow-y: auto;
    width: 100%;
    flex: 1 1 auto;
    .contain-editor {
      width: 100%;
      height: 500px;
      display: flex;
      flex-direction: column;
      .editor-nav {
        margin-bottom: 20px;
        span {
          color: #606266;
          font-size: 14px;
          padding-left: 12px;
        }
      }
      .editor {
        flex: 1 1 auto;
      }
    }
  }
}
</style>